package com.unitedats.common.Servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.unitedats.common.util.*;


public class FileViewerServlet extends HttpServlet {

    private static final int BUFFER_SIZE = 8 * 1024;

    // This implementation does not work
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		String fileName = WebUtil.decodeQueryStringParameter(request,"file_name");
		
        if (fileName != null) {

            byte[] buffer = new byte[BUFFER_SIZE];
            FileInputStream fis = null;
            OutputStream os = null;

            try {

                File f = new File(Default.FILES_UPLOAD_DIR + fileName);
                fis = new FileInputStream(f);

                response.setContentType("*/*");
                response.setHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");
                response.setContentLength((int) f.length());
                //fileName.substring(fileName.lastIndexOf(Default.FILE_NAME_SEPARATOR) + 1)
                //response.setHeader("Content-Length", String.valueOf(f.length()));
                os = response.getOutputStream();
                int byteRead = 0;

                while ((byteRead = fis.read(buffer)) != -1) {
                    os.write(buffer, 0, byteRead);
                    os.flush();
                }

            } catch (Exception ex) {
                Common.log(ex);
            } finally {
                if (fis != null)
                    fis.close();

                if (os != null)
                    os.close();

            }
        }
    }


    //    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //
    //
    //        String fileName = request.getParameter("file_name");
    //        if (fileName != null) {
    //
    //            response.setContentType("*/*");
    //            response.setHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");
    //            //fileName.substring(fileName.lastIndexOf(Default.FILE_NAME_SEPARATOR) + 1)
    //
    //            FileInputStream fis = null;
    //            OutputStream os = null;
    //
    //            try {
    //
    //                File f = new File(Default.FILES_UPLOAD_DIR + fileName);
    //                fis = new FileInputStream(f);
    //                byte[] fileBytes = new byte[(int) f.length()];
    //                fis.read(fileBytes, 0, fileBytes.length);
    //
    //                os = response.getOutputStream();
    //                os.write(fileBytes);
    //
    //            } catch (Exception ex) {
    //                Common.log(ex);
    //            } finally {
    //                if (fis != null)
    //                    fis.close();

    //                if (os != null)
    //                    os.close();
    //
    //            }
    //        }
    //    }


    // This implementation requires JDK 7
    //    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //
    //
    //        String fileName = request.getParameter("file_name");
    //        if (fileName != null) {
    //
    //            response.setContentType("*/*");
    //            response.setHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");
    //            //fileName.substring(fileName.lastIndexOf(Default.FILE_NAME_SEPARATOR) + 1)
    //
    //            OutputStream os = null;
    //            try {
    //                Path path = Paths.get(Default.FILES_UPLOAD_DIR + fileName);
    //                byte[] fileBytes = Files.readAllBytes(path);
    //                os = response.getOutputStream();
    //                os.write(fileBytes);
    //            } catch (Exception ex) {
    //                Common.log(ex);
    //            } finally {
    //                if (os != null)
    //                    os.close();
    //            }
    //        }
    //    }


    //    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
    //                                                                                          IOException {
    //        String fileName = request.getParameter("file_name");
    //        if (fileName != null) {
    //            byte[] fileBytes = getFileBytes(fileName);
    //            response.setContentType("*/*");
    //            response.setHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");
    //            OutputStream os = response.getOutputStream();
    //            os.write(fileBytes);
    //            os.close();
    //        }
    //    }


    //    public byte[] getFileBytes(String fileName) {
    //
    //        InputStream in = null;
    //        ByteArrayOutputStream byteOS = new ByteArrayOutputStream(1024);
    //        try {
    //            FileURLConnection conn;
    //            String urlStr = "file:///D:/uploads/" + fileName;
    //            URL url = new URL(urlStr);
    //            conn = (FileURLConnection) url.openConnection();
    //            //            conn.setRequestMethod("GET");
    //            byte[] data = new byte[1024];
    //
    //            in = conn.getInputStream();
    //            int length;
    //            while ((length = in.read(data)) != -1) {
    //                byteOS.write(data, 0, length);
    //            }
    //        } catch (IOException ex) {
    //            Common.log(ex);
    //        } finally {
    //            if (in != null) {
    //                try {
    //                    in.close();
    //                } catch (IOException ex) {
    //                    Common.log(ex);
    //                }
    //            }
    //        }
    //        return byteOS.toByteArray();
    //    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
